<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gas:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gas:company:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gas:company:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gas:company:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业统一信用代码" align="center" prop="companyCode" />
      <el-table-column label="父级企业统一信用代码" align="center" prop="companyCodeParent" />
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="行政区域编码" align="center" prop="areaCode" />
      <el-table-column label="街道" align="center" prop="street" />
      <el-table-column label="工商注册地址" align="center" prop="addressRegistry" />
      <el-table-column label="生产场所地址" align="center" prop="addressWorksite" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gas:company:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gas:company:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="220px">
        <el-form-item label="企业统一信用代码" prop="companyCode">
          <el-input v-model="form.companyCode" placeholder="请输入企业统一信用代码" />
        </el-form-item>
        <el-form-item label="父级企业统一信用代码" prop="companyCodeParent">
          <el-input v-model="form.companyCodeParent" placeholder="请输入父级企业统一信用代码" />
        </el-form-item>
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="行政区域编码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入行政区域编码" />
        </el-form-item>
        <el-form-item label="街道" prop="street">
          <el-input v-model="form.street" placeholder="请输入街道" />
        </el-form-item>
        <el-form-item label="工商注册地址" prop="addressRegistry">
          <el-input v-model="form.addressRegistry" placeholder="请输入工商注册地址" />
        </el-form-item>
        <el-form-item label="生产场所地址" prop="addressWorksite">
          <el-input v-model="form.addressWorksite" placeholder="请输入生产场所地址" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="representativePerson">
          <el-input v-model="form.representativePerson" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="法定代表人电话" prop="representativeMobile">
          <el-input v-model="form.representativeMobile" placeholder="请输入法定代表人电话" />
        </el-form-item>
        <el-form-item label="企业负责人" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入企业负责人" />
        </el-form-item>
        <el-form-item label="企业负责人手机" prop="responsibleMobile">
          <el-input v-model="form.responsibleMobile" placeholder="请输入企业负责人手机" />
        </el-form-item>
        <el-form-item label="安全负责人" prop="safetyResponsiblePersone">
          <el-input v-model="form.safetyResponsiblePersone" placeholder="请输入安全负责人" />
        </el-form-item>
        <el-form-item label="安全负责人手机" prop="safetyResponsibleMobile">
          <el-input v-model="form.safetyResponsibleMobile" placeholder="请输入安全负责人手机" />
        </el-form-item>
        <el-form-item label="安全管理人" prop="safetyManagePersone">
          <el-input v-model="form.safetyManagePersone" placeholder="请输入安全管理人" />
        </el-form-item>
        <el-form-item label="安全管理人手机" prop="safetyManageMobile">
          <el-input v-model="form.safetyManageMobile" placeholder="请输入安全管理人手机" />
        </el-form-item>
        <el-form-item label="安全值班电话" prop="dutyPhone">
          <el-input v-model="form.dutyPhone" placeholder="请输入安全值班电话" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="成立日期" prop="establishDate">
          <el-date-picker clearable
            v-model="form.establishDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择成立日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="企业网址" prop="webSite">
          <el-input v-model="form.webSite" placeholder="请输入企业网址" />
        </el-form-item>
        <el-form-item label="企业规模" prop="companyScale">
          <el-select v-model="form.companyScale" placeholder="请选择">
            <el-option
              v-for="item in companyScaleLabel"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属行业门类" prop="industryCategory">
          <el-input v-model="form.industryCategory" placeholder="请输入所属行业门类" />
        </el-form-item>
        <el-form-item label="所属行业大类" prop="industryClass">
          <el-input v-model="form.industryClass" placeholder="请输入所属行业大类" />
        </el-form-item>
        <el-form-item label="营业执照经营范围" prop="businessScope">
          <el-input v-model="form.businessScope" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="安全生产标准化等级" prop="safetyStandardGrad">
          <el-select v-model="form.safetyStandardGrad" placeholder="请选择">
            <el-option
              v-for="item in safetyStandardGradLabel"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安全生产许可证编号" prop="safetyLicenseNo">
          <el-input v-model="form.safetyLicenseNo" placeholder="请输入安全生产许可证编号" />
        </el-form-item>
        <el-form-item label="安全生产许可证有效期开始日期" prop="safetyLicenseStart">
          <el-date-picker clearable
            v-model="form.safetyLicenseStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择安全生产许可证有效期开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安全生产许可证有效期结束日期" prop="safetyLicenseEnd">
          <el-date-picker clearable
            v-model="form.safetyLicenseEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择安全生产许可证有效期结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="职工人数" prop="peopleEmployee">
          <el-input v-model="form.peopleEmployee" placeholder="请输入职工人数" />
        </el-form-item>
        <el-form-item label="是否在化工园区内" prop="inIndustrialPark">
          <el-select v-model="form.inIndustrialPark" placeholder="请选择">
            <el-option
              v-for="item in inIndustrialParkLabel"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属化工园区名称" prop="industrialParkName">
          <el-input v-model="form.industrialParkName" placeholder="请输入所属化工园区名称" />
        </el-form-item>
        <el-form-item label="厂区面积(默认为0)" prop="factoryArea">
          <el-input v-model="form.factoryArea" placeholder="请输入厂区面积(默认为0)" />
        </el-form-item>
        <el-form-item label="所属属地" prop="terriTory">
          <el-input v-model="form.terriTory" placeholder="请输入所属属地" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompany, getCompany, delCompany, addCompany, updateCompany } from "@/api/gas/company";

export default {
  name: "Company",
  data() {
    return {
      companyScaleLabel:[
        {
          "value":"1",
          "label":"大"
        },
        {
          "value":"2",
          "label":"中"
        },
        {
          "value":"3",
          "label":"小"
        },
        {
          "value":"4",
          "label":"微"
        }
      ],
      safetyStandardGradLabel:[
        {
          "value":"1",
          "label":"一级"
        },
        {
          "value":"2",
          "label":"二级"
        },
        {
          "value":"3",
          "label":"三级"
        }
      ],
      inIndustrialParkLabel:[
        {
          "value":"0",
          "label":"否"
        },
        {
          "value":"1",
          "label":"是"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyCode: null,
        companyCodeParent: null,
        companyName: null,
        areaCode: null,
        addressRegistry: null,
        addressWorksite: null,
        longitude: null,
        latitude: null,
        representativePerson: null,
        representativeMobile: null,
        responsiblePerson: null,
        responsibleMobile: null,
        safetyResponsiblePersone: null,
        safetyResponsibleMobile: null,
        safetyManagePersone: null,
        safetyManageMobile: null,
        dutyPhone: null,
        postCode: null,
        establishDate: null,
        webSite: null,
        companyScale: null,
        companyType: null,
        industryCategory: null,
        industryClass: null,
        businessScope: null,
        safetyStandardGrad: null,
        safetyLicenseNo: null,
        safetyLicenseStart: null,
        safetyLicenseEnd: null,
        peopleEmployee: null,
        inIndustrialPark: null,
        industrialParkName: null,
        companyStatus: null,
        factoryArea: null,
        terriTory: null,
        createName: null,
        updateName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        areaCode: [
          { required: true, message: "行政区域编码不能为空", trigger: "blur" }
        ],
        street: [
          { required: true, message: "街道不能为空", trigger: "blur" }
        ],
        addressRegistry: [
          { required: true, message: "工商注册地址不能为空", trigger: "blur" }
        ],
        addressWorksite: [
          { required: true, message: "生产场所地址不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
        representativePerson: [
          { required: true, message: "法定代表人不能为空", trigger: "blur" }
        ],
        responsiblePerson: [
          { required: true, message: "企业负责人不能为空", trigger: "blur" }
        ],
        responsibleMobile: [
          { required: true, message: "企业负责人手机不能为空", trigger: "blur" }
        ],
        safetyResponsiblePersone: [
          { required: true, message: "安全负责人不能为空", trigger: "blur" }
        ],
        safetyResponsibleMobile: [
          { required: true, message: "安全负责人手机不能为空", trigger: "blur" }
        ],
        dutyPhone: [
          { required: true, message: "安全值班电话不能为空", trigger: "blur" }
        ],
        companyScale: [
          { required: true, message: "企业规模(1 大 2 中 3 小 4 微)不能为空", trigger: "blur" }
        ],
        companyType: [
          { required: true, message: "企业类型(1 生产 2 经营 3 使用 4 化工 5 医药 6 其他)不能为空", trigger: "change" }
        ],
        industryCategory: [
          { required: true, message: "所属行业门类不能为空", trigger: "blur" }
        ],
        industryClass: [
          { required: true, message: "所属行业大类不能为空", trigger: "blur" }
        ],
        inIndustrialPark: [
          { required: true, message: "是否在化工园区内(0否 1是)不能为空", trigger: "blur" }
        ],
        industrialParkName: [
          { required: true, message: "所属化工园区名称不能为空", trigger: "blur" }
        ],
        companyStatus: [
          { required: true, message: "企业状态(企业状态类型：0正常，1停产，2涉密)不能为空", trigger: "change" }
        ],
        factoryArea: [
          { required: true, message: "厂区面积(默认为0)不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业信息列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyCode: null,
        companyCodeParent: null,
        companyName: null,
        areaCode: null,
        addressRegistry: null,
        addressWorksite: null,
        longitude: null,
        latitude: null,
        representativePerson: null,
        representativeMobile: null,
        responsiblePerson: null,
        responsibleMobile: null,
        safetyResponsiblePersone: null,
        safetyResponsibleMobile: null,
        safetyManagePersone: null,
        safetyManageMobile: null,
        dutyPhone: null,
        postCode: null,
        establishDate: null,
        webSite: null,
        companyScale: null,
        companyType: null,
        industryCategory: null,
        industryClass: null,
        businessScope: null,
        safetyStandardGrad: null,
        safetyLicenseNo: null,
        safetyLicenseStart: null,
        safetyLicenseEnd: null,
        peopleEmployee: null,
        inIndustrialPark: null,
        industrialParkName: null,
        companyStatus: null,
        factoryArea: null,
        terriTory: null,
        createTime: null,
        createName: null,
        updateTime: null,
        updateName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业信息编号为"' + ids + '"的数据项？').then(function() {
        return delCompany(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gas/company/export', {
        ...this.queryParams
      }, `company_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
